package org.example.week11.config;

import org.example.week11.pubsub.RedisMessagePublisher;
import org.example.week11.pubsub.RedisMessageSubscriber;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

//@Configuration
public class MyPubSubConfig {

    @Bean
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter(new RedisMessageSubscriber());
    }

    @Bean
    RedisMessagePublisher redisPublisher(RedisTemplate<Object, Object> redisTemplate,ChannelTopic topic) {
        return new RedisMessagePublisher(redisTemplate, topic);
    }

    @Bean
    ChannelTopic topic() {
        return new ChannelTopic("orderQueue");
    }

    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory,
                                                 MessageListenerAdapter listenerAdapter,
                                                 ChannelTopic topic) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, topic);
        return container;
    }
}
